package com.zxy.supplier_system.server.service

import com.zxy.supplier_system.server.controller.CompositeGoodsController
import com.zxy.supplier_system.server.entity.CompositeGoods
import com.zxy.supplier_system.server.entity.Goods
import com.zxy.supplier_system.server.repository.CompositeGoodsRepository
import com.zxy.supplier_system.server.repository.GoodsRepository
import com.zxy.supplier_system.server.repository.SubGoodsRepository
import com.zxy.supplier_system.server.utils.orElse404
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class CompositeGoodsService(
    private val goodsRepository: GoodsRepository,
    private val compositeGoodsRepository: CompositeGoodsRepository,
    private val subGoodsRepository: SubGoodsRepository,
    private val barcodeGenerator: CompositeGoodsBarcodeGenerator,
    private val systemServiceService: SystemServiceService
) {
    @Transactional
    fun createCompositeGoods(request: CompositeGoodsController.AddCompositeGoodsRequest): Goods {
        // 创建组合商品主体
        val compositeGoods = Goods().apply {
            name = request.name
            price = request.price
            note = request.note
            barcode = barcodeGenerator.generate()
            this.systemService = systemServiceService.getCurrentSystemService()
        }

        // 保存组合商品主体
        val savedGoods = goodsRepository.save(compositeGoods)

        // 创建组合项
        val compositeItems = request.items.mapNotNull { item ->
            when {
                item.goodsId != null -> {
                    val goods = goodsRepository.findByIdOrNull(item.goodsId).orElse404(null)
                    CompositeGoods().apply {
                        this.sourceGoods = savedGoods
                        this.goods = goods
                        this.amount = item.amount
                    }
                }

                item.subGoodsId != null -> {
                    val subGoods = subGoodsRepository.findByIdOrNull(item.subGoodsId).orElse404(null)
                    CompositeGoods().apply {
                        this.sourceGoods = savedGoods
                        this.subGoods = subGoods
                        this.amount = item.amount
                    }
                }

                else -> null
            }
        }

        // 保存组合项
        compositeGoodsRepository.saveAll(compositeItems)

        return savedGoods
    }

    @Transactional
    fun updateCompositeGoods(id: Int, request: CompositeGoodsController.AddCompositeGoodsRequest): Goods {
        // 获取现有组合商品
        val compositeGoods = goodsRepository.findByIdOrNull(id).orElse404()

        // 更新基本信息
        compositeGoods.name = request.name
        compositeGoods.price = request.price
        compositeGoods.note = request.note

        // 删除原有组合项
        compositeGoodsRepository.deleteBySourceGoods(compositeGoods)

        // 创建新组合项
        val compositeItems = request.items.mapNotNull { item ->
            when {
                item.goodsId != null -> {
                    val goods = goodsRepository.findByIdOrNull(item.goodsId).orElse404(null)
                    CompositeGoods().apply {
                        this.sourceGoods = compositeGoods
                        this.goods = goods
                        this.amount = item.amount
                    }
                }

                item.subGoodsId != null -> {
                    val subGoods = subGoodsRepository.findByIdOrNull(item.subGoodsId).orElse404(null)
                    CompositeGoods().apply {
                        this.sourceGoods = compositeGoods
                        this.subGoods = subGoods
                        this.amount = item.amount
                    }
                }

                else -> null
            }
        }

        // 保存新组合项
        compositeGoodsRepository.saveAll(compositeItems)

        // 更新组合商品
        return goodsRepository.save(compositeGoods)
    }


    @Transactional(readOnly = true)
    fun getCompositeGoodsDetail(id: Int): CompositeGoodsController.CompositeGoodsDetail {
        val goods = this.goodsRepository.findByIdAndSystemServiceAndDisableIsFalse(id,systemServiceService.getCurrentSystemService()).orElse404()

        return CompositeGoodsController.CompositeGoodsDetail(
            goods.id,
            goods.name,
            goods.price,
            goods.note,
            goods.barcode,
            goods.compositeGoodsList.map {
                val goods = it.goods ?: it.subGoods?.goods!!
                CompositeGoodsController.CompositeGoodsDetail.Item(
                    it.goods!!.id,
                    it.subGoods?.id,
                    it.amount,
                    goods.barcode,
                    it.goods!!.name,
                    it.subGoods?.name,
                    it.subGoods?.number
                )
            }
        )
    }
}